package com.eipc;

import org.androidpn.sdk.Constants;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.eipc.info.R;
import com.eipc.model.ImportNotice;
import com.eipc.model.SerInfoResponse;
import com.eipc.util.DBHelper;
import com.eipc.util.HttpManager;
import com.eipc.util.JsonParse;

public class ImportNoticeActivity extends CommonTabActivity {
	public final static int TYPE = DBHelper.APP_TYPE_IMPORT_NOTICE;
	private PollingTask mTask;
	private TabSpec onTab;
	private TabSpec twoTab;
	private TabSpec threeTab;
	private int mType;
	private int TYPE_ONE = 0;
	private int TYPE_TWO = 1;
	private int TYPE_THR = 2;
	private boolean isFi = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.import_notice);
        initView();
        
        mType = getIntent().getIntExtra("type",0);
        mTabHost.setCurrentTab(mType);  
	}
    
    protected void initView(){
    	super.initView();
    	mBar.setMiddleText(R.string.import_notice);
    	mBar.setOnRightClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
//				mTask = new PollingTask();
//				mTask.execute();
				
//				Log.i("StartService","FetchPerson");
//				startService( new Intent(ImportNoticeActivity.this, FetchPersonService.class));
				
				Log.i("StartService","FetchNotice");				
				startService( new Intent(ImportNoticeActivity.this, FetchNoticeService.class));
				
			}
		});
    	mTabHost = getTabHost();
    	mTabHost.setFadingEdgeLength(10);
    	Intent i = new Intent(this,ReceiveActivity.class);
    	i.putExtra("type", TYPE);
    	onTab = mTabHost.newTabSpec("tab1");
    	onTab.setIndicator(buildLabel(R.string.import_notice_receive)).setContent(i);
    	mTabHost.addTab(onTab);
    	i = new Intent(this,SendActivity.class);
	    i.putExtra("type", TYPE);
//	    i.putExtra("leader", mPerson.isLeader);
	    twoTab = mTabHost.newTabSpec("tab2");
	    twoTab.setIndicator(buildLabel(R.string.import_notice_send)).setContent(i);
	    mTabHost.addTab(twoTab);
	    i = new Intent(this,ReceivedActivity.class);
	    i.putExtra("type", TYPE);
	    threeTab = mTabHost.newTabSpec("tab3");
	    threeTab.setIndicator(buildLabel(R.string.import_notice_received)).setContent(i);
	    mTabHost.addTab(threeTab);    	
    }
    
    protected Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 0:
					break;
				case 1:
					break;
				case 2:
					break;
			}
		};
	};
	
    private TabHost mTabHost;
    
    class PollingTask extends CommonTask<Void>{
		public PollingTask(){
			super(ImportNoticeActivity.this);
		}
		private String mResult;
		@Override
		protected Void doInBackground(Void... params) {
			try {
				SharedPreferences sdkPreferences = getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE);
				String mobileID = sdkPreferences.getString(Constants.XMPP_USERNAME,"");
				String maxID    = sdkPreferences.getString(Constants.MaxId,"0");
				String strImids = mDB.getIMIDs();
				Log.i("IDS for IM", strImids);
//				mResult = HttpManager.polling(getString(R.string.url_polling),Constants.TTYPE_NOTICE,maxID,ids,mobileID,"0");
//				Log.e("------------refesh notices-------------",String.valueOf(mResult));
//				if(mResult!=null && !"".equals(mResult)){
//					Log.e("------------refesh notices-------------",String.valueOf(mResult));
//					SerInfoResponse resp = JsonParse.parseSerInfoResponse(mResult,Constants.LOAD_INPORTNOTICE);
//					Log.e("------------refesh SerInfoResponse-------------",String.valueOf(resp));
//					if(resp!=null&&resp.success&&resp.os!=null&&resp.os.length>0){
//						if(resp.id!=null&&!"".equals(resp.id)){
//							mDB.updateMaxId(resp.id);
//							try{mDB.insertImportNotice((ImportNotice[])resp.os,resp.id);}catch(Exception e){}
//						}
//					}else{
//							Log.e(this.getClass().getName()+" polling failure : ",resp.toString());
//					}
//				}else{
//					Log.e(this.getClass().getName()+" polling failure : ","mResult is null");
//				}
				
			} catch (Exception e) {
//				MyLog.error(FetchConsultService.class,"",e);
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result){
			super.onPostExecute(result);
				Intent i = new Intent(ImportNoticeActivity.this,ImportNoticeActivity.class);
				startActivity(i);
				finish();
		}
		
	}
    
    @Override
    protected void updateView(){
    	super.updateView();
    	isFi = false;
    	Intent i = new Intent(ImportNoticeActivity.this,ImportNoticeActivity.class);
		startActivity(i);
		finish();
    }
}
